package com.yang.zngcxyxkjs.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yang.zngcxyxkjs.mapper.CompetitionMapper;
import com.yang.zngcxyxkjs.pojo.Competition;
import com.yang.zngcxyxkjs.pojo.Student;
import com.yang.zngcxyxkjs.pojo.Teacher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class CompetitionController {

    @Autowired
    private CompetitionMapper competitionMapper;


    //TODO Student
    @RequestMapping("/getAllCompetitionsByPages")
    public String getAllCompetitions(@RequestParam(defaultValue = "1") Integer currentPageNum, Model model, HttpSession session,Integer f){
        PageHelper.startPage(currentPageNum,5);
        List<Competition> list = competitionMapper.selectAllCompetitions();
        PageInfo<Competition> pageInfo = new PageInfo<>(list);
//        System.out.println(pageInfo);

        Student stu = (Student)session.getAttribute("stu");
        model.addAttribute("stu",stu);

        model.addAttribute("f",f);

        model.addAttribute("pageInfo",pageInfo);
        return "competition/AllCompetitions.html";
    }

    @RequestMapping("/getCompetitionsByConditions")
    public String getCompetitionsByConditions(String comName,String dutyTeacher,Model model,HttpSession session){
        List<Competition> list = competitionMapper.selectCompetitionsByConditions(comName,dutyTeacher);
        PageInfo<Competition> pageInfo = new PageInfo<>(list);
//        System.out.println(pageInfo);

        Student stu = (Student)session.getAttribute("stu");
        model.addAttribute("stu",stu);

        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("comName",comName);
        model.addAttribute("dutyTeacher",dutyTeacher);
        return "competition/AllCompetitions.html";

    }

    @RequestMapping("/getCompetitionBycomId")
    public String getCompetitionBycomId(@RequestParam Integer comId,Model model,HttpSession session){
        Competition competition = competitionMapper.selectCompetitionBycomId(comId);
        model.addAttribute(competition);
//        System.out.println("-----------------zhe");
//        System.out.println(competition);
//        System.out.println("-----------------zhe");

        Student stu = (Student)session.getAttribute("stu");
        model.addAttribute("stu",stu);

        return "competition/getCompetitionById.html";
    }


    //TODO Teacher
    @RequestMapping("/teaGetAllCompetitionsByPages")
    public String teaGetAllCompetitions(@RequestParam(defaultValue = "1") Integer currentPageNum, Model model, HttpSession session){
        Teacher tea = (Teacher) session.getAttribute("tea");
        model.addAttribute("tea",tea);
        PageHelper.startPage(currentPageNum,5);
        List<Competition> list = competitionMapper.selectAllCompetitions();
        PageInfo<Competition> pageInfo = new PageInfo<>(list);
//        System.out.println(pageInfo);

//        Student stu = (Student)session.getAttribute("stu");
//        model.addAttribute("stu",stu);

        model.addAttribute("pageInfo",pageInfo);
        return "teacher/teacherAllCompetitions.html";
    }

    @RequestMapping("/teaGetCompetitionsByConditions")
    public String teaGetCompetitionsByConditions(String comName,String dutyTeacher,Model model,HttpSession session){
        Teacher tea = (Teacher) session.getAttribute("tea");
        model.addAttribute("tea",tea);
        List<Competition> list = competitionMapper.selectCompetitionsByConditions(comName,dutyTeacher);
        PageInfo<Competition> pageInfo = new PageInfo<>(list);
//        System.out.println(pageInfo);

//        Student stu = (Student)session.getAttribute("stu");
//        model.addAttribute("stu",stu);

        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("comName",comName);
        model.addAttribute("dutyTeacher",dutyTeacher);
        return "teacher/teacherAllCompetitions.html";
    }

//    @RequestMapping("/teaGetCompetitionBycomId")
//    public String teaGetCompetitionBycomId(@RequestParam Integer comId,Model model,HttpSession session){
//        Teacher tea = (Teacher) session.getAttribute("tea");
//        model.addAttribute("tea",tea);
//        Competition competition = competitionMapper.selectCompetitionBycomId(comId);
//        model.addAttribute(competition);
////        System.out.println("-----------------zhe");
////        System.out.println(competition);
////        System.out.println("-----------------zhe");
//
////        Student stu = (Student)session.getAttribute("stu");
////        model.addAttribute("stu",stu);
//
//        return "teacher/teacherGetCompetitionById.html";
//    }

}
